/**
 * Copyright (c) 2006-2007 geolabs
 * author : Gérald Fenoy
 *
 * Map gesture
 *
 */

var img = new Array("zoom_in","zoom_out","recenter","move","query_point");

function setForm(){
    var hasValue=false;
    for(i=0;i<=(img.length-1);i++)
	if(img[i].toUpperCase()==document.myform.CMD.value){
	    hasValue=true;
	    eval("document."+img[i]+".src='./images/"+img[i]+"_down.gif'");
	}
    	else
	    eval("document."+img[i]+".src='./images/"+img[i]+"_up.gif'");
    if(!hasValue){
	eval("document."+img[0]+".src='./images/"+img[0]+"_down.gif'");
	document.myform.CMD.value = 'ZOOM_IN';
	document.myform.CMD.checked = true;
    }
}

function uncheck(inp){
    unset(inp);
    eval('document.myform.'+inp+'.checked=false;');
}

function unset(inp){
    eval('document.myform.'+inp+'.value=\'\';');    
}

/**
 * The OctoMap Class
 */
System.require("Position");

OctoMap=Class.create();
OctoMap.define({

  /**
   * mic = MoveInsideContainer
   */
  mic: false,
  A: null,
  B: null,
  boxHObj: null,
  hasBoxInitial: false,
  inMovement: false,
  isOrigMovement: false,
  isActivate: false,

  _init: function(){
    this.container=arguments[0];
    this.name=this.container.id;
    
    this.size= new Position(0,0);
    this.gsize= new Position(0,0);
    /**
     * The html relative origin coordinate (110,120)
     */
    this.begin=new Position(110,120);

    /**
     * The origin coordinate (0,0)
     */
    this.orig=new Position(0,0);
    this.move=new Position(0,0);
    this.mouse=new Position(0,0);

    /**
     * box = { pos: { X , Y }, size: { Width, Height } }
     */
    this.box={};
    this.box.pos=new Position(0,0);
    this.box.size=new Position(0,0);

    this.initial=new Position(0,0);

    /**
     * boxHasNegative = { boxHasNegativeX , boxHasnegativeY }
     */
    this.boxHasNegative= new Array(0,0);

    /**
     * Geometric coordinates
     */
    this.min=new Position(0,0);
    this.max=new Position(0,0);

    },

    /**
     * Initialization functions.
     */
    initializeValues: function(){
	this.min.x = parseFloat(document.myform.minx.value);
	this.min.y = parseFloat(document.myform.miny.value);
	this.max.x = parseFloat(document.myform.maxx.value);
	this.max.y = parseFloat(document.myform.maxy.value);
	this.size.x=parseFloat(document.myform.imagewidth.value);
	this.size.y=parseFloat(document.myform.imageheight.value);
	this.gsize.x=Math.abs(this.max.x-this.min.x);
	this.gsize.y=Math.abs(this.max.y-this.min.y);
    },
    
    init: function(obj){

	if(document.myform.CMD.value=='MOVE')
	    document.getElementById(this.name).style.cursor='move';
	else if(document.myform.CMD.value=='QUERY_POINT')
	    document.getElementById(this.name).style.cursor='help';
	else
	    document.getElementById(this.name).style.cursor='crosshair';

	this.initializeValues();
	this.container = obj;
	this.inMovement=false;
	this.isActivate=false;
	var tmp=Position.fromElement(this.container);
	this.begin.x=tmp.x;
	this.begin.y=tmp.y;
	this.initial.x=0;
	this.initial.y=0;
	this.container.onmousemove=this.update.bind(this);
	if(document.myform.CMD.value == 'RECENTER' || 
	   document.myform.CMD.value == 'ZOOM_OUT')
	    {
	      this.container.onclick=this.activate.bind(this);
	      this.container.onmousedown = this.doNothing.bind(this);
	      this.container.onmouseup = this.doNothing.bind(this);
	    }
	else if(document.myform.CMD.value == 'MOVE'){
	  this.container.onmousedown=new Function(this.name+".inMovement=true;"+this.name+".isOrigMovement=true;");
	  this.container.onmouseup = this.endMove.bind(this);
	  this.container.onclick = this.doNothing.bind(this);
	}
	else{
	  this.container.onmousedown = this.setBox.bind(this);
	  this.container.onmouseup = this.setBox.bind(this);
	  this.container.onclick = this.doNothing.bind(this);
	}
    },
    /** -------------------------------------------------------------- **/

    /**
     * Actions on mouse events.
     */
    doNothing: function(e){
    },

    update: function(e)
    {
      this.initializeValues();
      this.getMouseXY(e);
      var tmp=new Array();
      tmp[0]=Math.abs(this.mouse.x-this.begin.x);
      tmp[1]=Math.abs(this.mouse.y-this.begin.y);
      tmp=this.convert2Geo(tmp);
      document.getElementById("currX").innerHTML=parseInt(this.min.x+tmp[0]);
      document.getElementById("currY").innerHTML=parseInt(this.min.y+tmp[1]);
    },

    activate: function(e){
      this.isActivate=true;
      this.setBoxOrig(e);
      this.setBoxFinal(false);
    },

    setBox: function(e){
      if(!this.hasBoxInitial)
	this.setBoxOrig(e);
      else{
	this.setBoxEnd(e);
	//alert(document.getElementById("currX").innerHTML+" "+document.getElementById("currY").innerHTML);
	this.setBoxFinal(true);


      }
    },

    endMove: function(e){
      this.inMovement=false;
      this.box.pos.x=(this.size.x/2)-this.move.x;
      this.box.pos.y=(this.size.y/2)-this.move.y;
      this.boxHObj=document.createElement('div');
      this.container.appendChild(this.boxHObj);
      document.myform.CMD.value='RECENTER';
      setTimeout("document.myform.CMD.value='MOVE';",1000);
      this.setBoxFinal(false);
    },
    
    /** -------------------------------------------------------------- **/

    /**
     * Get the event's coorindates
     */
    getCoordinate: function(e){
      var tmp=Position.fromEvent(e);
      return Array(tmp.x,tmp.y);
    },
    /** -------------------------------------------------------------- **/

    /**
     * Convertion function
     */
    convert2Geo: function(a){
	var R=new Array();
	R[0] = a[0] * (this.gsize.x / this.size.x);
	R[1] = this.gsize.y - (a[1] * (this.gsize.y / this.size.y));
	return R;
    },
    /** -------------------------------------------------------------- **/
    //          boxWidth
    //  BoxOrig-----------------B
    //  |                       |
    //  |                       |
    //  |                       |  boxHeight
    //  |                       |
    //  |                       |
    //  A-----------------------BoxEnd
    //
    getMouseXY: function(e)
    {
      this.mouse=Position.fromEvent(e);//this.getCoordinate(e);
	
	if(this.inMovement)
	  {
	    this.boxHObj=null;
	    if(this.isOrigMovement){
	      this.isOrigMovement=false;		    
	      this.orig=this.mouse;
	    }
	    else{
	      this.move.x=this.mouse.x-this.orig.x;
	      this.move.y=this.mouse.y-this.orig.y;
	      this.container.style.backgroundPosition= this.move.x + ' ' + this.move.y;
	    }
	  }
	if(this.boxHObj)
	  this.setBoxEnd(e);
    },
    
    setA: function(){
      this.A = new Array();
      if(this.boxHasNegative[0])
	this.A[0] = this.box.pos.x - this.box.size.x;
      else
	this.A[0] = this.box.pos.x; //X coordinate
      if(this.boxHasNegative[1])
	this.A[1] = this.box.pos.y;
      else
	this.A[1] = this.box.pos.y + this.box.size.y; //Y Coordinate
    },
      
    setB: function(){
	this.B = new Array();
	if(this.boxHasNegative[0])
	  this.B[0] = this.box.pos.x;
	else
	  this.B[0] = this.box.pos.x + this.box.size.x; //X coordinate
	if(this.boxHasNegative[1])
	    this.B[1] = this.box.pos.y - this.box.size.y;
	else
	    this.B[1] = this.box.pos.y; //Y Coordinate
    },

    setBoxOrig: function(e)
    {
	this.hasBoxInitial=true;
	var tmp=this.getCoordinate(e);
	this.box.pos.x=tmp[0];
	this.box.pos.y=tmp[1];
	this.boxHObj = document.createElement('div');
	this.boxHObj.id = "box";
	this.container.appendChild(this.boxHObj);
	if(!this.isActivate){
	    this.boxHObj.style.left = (this.box.pos.x-this.begin.x) + 'px';
	    this.boxHObj.style.top = (this.box.pos.y-this.begin.y) + 'px';
	    this.boxHObj.onmousemove = this.update.bind(this);
	    this.boxHObj.onmouseup = this.setBox.bind(this);
	}
    },
    
    setBoxEnd: function(e){
	this.setBoxEndCoordinate(e);
	
	this.boxHObj.style.zIndex = this.begin.y;
	
	this.boxHasNegative[0]= (this.box.pos.x<this.mouse.x?false:true);
	this.boxHasNegative[1]= (this.box.pos.y<this.mouse.y?false:true);
	if(!this.isActivate){
	    this.boxHObj.style.left = ((this.box.pos.x<this.mouse.x?this.box.pos.x:this.mouse.x)-this.begin.x) + 'px';
	    this.boxHObj.style.top = ((this.box.pos.y<this.mouse.y?this.box.pos.y:this.mouse.y)-this.begin.y) + 'px';
	    this.boxHObj.style.width = this.box.size.x + 'px';
	    this.boxHObj.style.height = this.box.size.y + 'px';
	    this.boxHObj.onclick=this.setBox.bind(this);
	}
    },
    
    setBoxFinal: function(t){
	this.boxHObj.parentNode.removeChild(this.boxHObj);
	this.boxHObj = null;
	this.hasBoxInitial = false;
	
	if(document.myform.CMD.value=='QUERY_POINT' || 
	   document.myform.CMD.value=='ZOOM_IN'){
	    this.setA();
	    this.setB();
	    
	    if(!document.myform.INPUT_TYPE)
		{
		    var inp = document.createElement('input');
		    inp.type = 'hidden';
		    inp.name = 'INPUT_TYPE';
		    inp.id = 'INPUT_TYPE';
		    inp.value = '';
		    this.container.parentNode.appendChild(inp);
		    inp = document.createElement('input');
		    inp.type = 'hidden';
		    inp.name = 'INPUT_COORD';
		    inp.id = 'INPUT_COORD';
		    inp.value = '';
		    this.container.parentNode.appendChild(inp);
		}
	    document.myform.INPUT_TYPE.value = 'auto_rect';
	    document.myform.INPUT_COORD.value = (this.A[0]-this.begin.x) + ',' + (this.A[1]-this.begin.y) + ';' + (this.B[0]-this.begin.x) + ',' + (this.B[1]-this.begin.y) ;
	    this.container.style.cursor='wait';
	}
	else{
	    if(document.myform.INPUT_TYPE)
		{
		    document.myform.INPUT_TYPE.value = '';
		    document.myform.INPUT_COORD.value = '';
		}
	}
	if(t)
	    {
		document.myform.mainmap_x.value = this.box.pos.x + (this.box.size.x/2) - this.begin.x  ;
		document.myform.mainmap_y.value = this.box.pos.y + (this.box.size.y/2) - this.begin.y ;
	    }
	else{
	    if(this.isActivate){
		document.myform.mainmap_x.value = this.box.pos.x - this.begin.x ;
		document.myform.mainmap_y.value = this.box.pos.y - this.begin.y ;
	    }
	    else{
		document.myform.mainmap_x.value = this.box.pos.x;
		document.myform.mainmap_y.value = this.box.pos.y;
	    }
	}

	if(document.getElementById("CMD").value=='segmentize'){
	  document.myform.mainmap_x.value = document.getElementById("currX").innerHTML;
	  document.myform.mainmap_y.value = document.getElementById("currY").innerHTML;
	}

	document.myform.mainmap_v.value = 'true' ;
	waitLoading();
    },
    
    setBoxInitialCoordinate: function(e){
      this.box=Position.fromEvent(e);
    },
    
    setBoxEndCoordinate: function(e){
	if(this.mouse.x > this.box.pos.x)
	    this.box.size.x = this.mouse.x - this.box.pos.x;
	else
	    this.box.size.x = this.box.pos.x - this.mouse.x;
	if(this.mouse.y > this.box.pos.y)
	    this.box.size.y = this.mouse.y - this.box.pos.y;
	else
	    this.box.size.y = this.box.pos.y - this.mouse.y;
    }
      
});
